package com.jinunn.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.jinunn.hosp.repository.HospitalRepository;
import com.jinunn.hosp.service.HospitalService;
import com.jinunn.yygh.cmn.client.DictFeignClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : JinDun
 * @date : 2021/4/27 23:46
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> switchMap) {
        //把参数Map集合转换为Hospital
        String jsonString = JSONObject.toJSONString(switchMap);
        Hospital hospital = JSONObject.parseObject(jsonString, Hospital.class);

        //判断数据在数据库中是否存在
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());

        //判断是否存在
        if (hospitalExist!=null){
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setUpdateTime(new Date());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else {
            hospital.setStatus(0);
            hospital.setUpdateTime(new Date());
            hospital.setCreateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    @Override
    public Page<Hospital> seleteHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //创建pageable对象
        Pageable pageable = PageRequest.of(page-1,limit);
        //创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        //创建条件对象
        Example<Hospital> example = Example.of(hospital,matcher);

        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        pages.getContent().forEach(this::setHospitalHosType);
        return pages;
    }

    @Override
    public void updateHospStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String, Object> showDetail(String id) {
        Map<String,Object> result = new HashMap<>();
        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());

        result.put("hospital",hospital);
        result.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return result;
    }

    @Override
    public List<Hospital> findHospName(String hospName) {
        return hospitalRepository.findHospitalByHosnameLike(hospName);
    }

    @Override
    public Map<String, Object> getRegisteredDetail(String hoscode) {
        Map<String,Object> result = new HashMap<>();
        //获取医院详情
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
        //获取预约规则
        result.put("hospital",hospital);
        result.put("bookingRule",hospital.getBookingRule());
        return result;
    }


    private Hospital setHospitalHosType(Hospital hospital) {
        //根据dictCode和Value获取医院等级
        String hostypeString = dictFeignClient.getDictName("Hostype", hospital.getHostype());

        //查询省、市、地区
        String provinceString = dictFeignClient.getDictName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getDictName(hospital.getCityCode());
        String districtString = dictFeignClient.getDictName(hospital.getDistrictCode());

        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        hospital.getParam().put("hostypeString",hostypeString);
        return hospital;
    }
}
